<?php

/**
 * 不验证token，开发的api接口
 */
namespace app\api\controller;


use app\common\bean\ListMap;
use app\common\manage\WechatManage;
use app\common\utils\ExitJsonUtil;
use Exception;
use think\Controller;

class OpenApi extends Controller
{
    /**
     * 获取微信授权链接
     */
    public function userAuth(){
        $redirectUrl = request()->param('redirect_url');
        WeChatManage::getInstance()->publicWechatAuth($redirectUrl);
    }

    /**微信公众号授权回调
     * @param $goUrl
     */
    public function wechatCallback($goUrl){
        $userBean = WeChatManage::getInstance()->publicWechatCallback();
        $temp = strstr(base64_decode($goUrl),'?') ? '&' : '?';
        $url = base64_decode($goUrl).$temp.http_build_query($userBean->getData());
        exit("<script>location.href='{$url}';</script>");
    }

    /**
     * 小程序根据code获取用户信息
     * @throws Exception
     */
    public function userAuthApplet(){
        $listMap = new ListMap();
        $listMap->setData(request()->param());
        $result = WeChatManage::getInstance()->userAuthApplet($listMap);
        ExitJsonUtil::getInstance()->exitData($result->getData());
    }

    /**
     * 打印参数
     */
    public function printParam(){
        var_dump(request()->param());
    }

    /**
     * 小程序根据code获取用户信息
     * @throws Exception
     */
    public function getUserByCode(){
        $code = request()->param('code');
        $result = WeChatManage::getInstance()->getUserByCode($code);
        ExitJsonUtil::getInstance()->exitData($result);
    }
}